﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Marmat.Template.WinForm
{

    /// <summary>
    /// 列实体的集合
    /// </summary>
    public class ColumnEntityCollection : IList<ColumnEntity>
    {
        private List<ColumnEntity> collection = new List<ColumnEntity>();


        /// <summary>
        /// 获取或设置指定索引处的元素。
        /// </summary>
        /// <param name="index">要获取或设置的元素的从零开始的索引。</param>
        /// <returns>指定索引处的元素。</returns>
        public ColumnEntity this[int index] { get => this.collection[index]; set => this.collection[index] = value; }

        /// <summary>
        /// 获取 System.Collections.Generic.List`1 中包含的元素数。
        /// <returns>System.Collections.Generic.List`1 中包含的元素数。</returns>
        /// </summary>
        public int Count => this.collection.Count;

        /// <summary>
        /// 获取一个值，该值指示 System.Collections.Generic.ICollection`1 是否为只读。
        /// </summary>
        bool ICollection<ColumnEntity>.IsReadOnly => false;

        /// <summary>
        /// 添加列。
        /// </summary>
        /// <param name="name"></param>
        public void Add(string name)
        {
            ColumnEntity item = new ColumnEntity();
            item.Name = name;
            this.collection.Add(item);
        }

        /// <summary>
        /// 添加列。
        /// </summary>
        /// <param name="item"></param>
        public void Add(ColumnEntity item)
        {
            this.collection.Add(item);
        }

        /// <summary>
        /// 从 System.Collections.Generic.List`1 中移除所有元素。
        /// </summary>
        public void Clear()
        {
            this.collection.Clear();
        }

        /// <summary>
        /// 确定某元素是否在 System.Collections.Generic.List`1 中。
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(ColumnEntity item)
        {
            return this.collection.Contains(item);
        }

        /// <summary>
        /// 通过名称寻找元素
        /// </summary>
        /// <param name="name">元素名称</param>
        /// <returns></returns>
        public ColumnEntity Find(string name)
        {
            return this.collection.Find(x => x.Name == name);
        }

        /// <summary>
        /// 从目标数组的指定索引处开始，将整个 System.Collections.Generic.List`1 复制到兼容的一维数组。
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(ColumnEntity[] array, int arrayIndex)
        {
            this.collection.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// 返回循环访问 System.Collections.Generic.List`1 的枚举数。
        /// </summary>
        /// <returns></returns>
        public IEnumerator<ColumnEntity> GetEnumerator()
        {
            return this.collection.GetEnumerator();
        }

        /// <summary>
        /// 搜索指定的对象，并返回整个 System.Collections.Generic.List`1 中第一个匹配项的从零开始的索引。
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int IndexOf(ColumnEntity item)
        {
            return this.collection.IndexOf(item);
        }

        /// <summary>
        /// 将元素插入 System.Collections.Generic.List`1 的指定索引处。
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        public void Insert(int index, ColumnEntity item)
        {
            this.collection.Insert(index, item);
        }

        /// <summary>
        /// 从 System.Collections.Generic.List`1 中移除特定对象的第一个匹配项。
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(ColumnEntity item)
        {
            return this.collection.Remove(item);
        }

        /// <summary>
        /// 移除 System.Collections.Generic.List`1 的指定索引处的元素。
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            this.collection.RemoveAt(index);
        }

        /// <summary>
        /// 返回循环访问 System.Collections.Generic.List`1 的枚举数。
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public static implicit operator ColumnEntityCollection(string[] value)
        {
            ColumnEntityCollection collection = new ColumnEntityCollection();
            foreach (var item in value)
            {
                ColumnEntity column = new ColumnEntity();
                column.Name = item;
                collection.Add(column);
            }
            return collection;
        }
    }
}
